package com.netease.nim.demo.team.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.netease.nim.demo.base.YXBaseActivity;
import com.shwread.android.qysw10000227.R;

import logic.util.Utils;

/**
 * Created by hzxuwen on 2015/3/19.
 */
public class TeamNicknameActivity extends YXBaseActivity implements OnClickListener {

    // constant
    public static final String EXTRA_NAME = "EXTRA_NAME";
    public static final int REQ_CODE_TEAM_NAME = 20;

    // view
    private EditText regularTeamNickname;

    // data
    private String nickName;
	private TextView tvTitle;
	private Button btnBack;
	private Button btnSubmit;

    public static void start(Context context, String name) {
        Intent intent = new Intent();
        intent.setClass(context, TeamNicknameActivity.class);
        intent.putExtra(EXTRA_NAME, name);
        ((Activity)context).startActivityForResult(intent, REQ_CODE_TEAM_NAME);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.team_nickname_activity);

        nickName = getIntent().getStringExtra(EXTRA_NAME);
        if (nickName == null) {
            nickName = "";
        }
        tvTitle = (TextView) findViewById(R.id.common_header_name);
		tvTitle.setVisibility(View.VISIBLE);
		tvTitle.setText("修改群名称");
		btnBack = (Button) findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.VISIBLE);
		btnBack.setOnClickListener(this);
		btnSubmit = (Button) findViewById(R.id.common_header_publish);
		btnSubmit.setVisibility(View.VISIBLE);
		btnSubmit.setText("提交");
		btnSubmit.setOnClickListener(this);
        regularTeamNickname = (EditText) findViewById(R.id.regular_team_nickname);
        regularTeamNickname.setText(nickName);
        if(!TextUtils.isEmpty(nickName)){
        	regularTeamNickname.setSelection(regularTeamNickname.getText().length());
        }
        regularTeamNickname.setOnKeyListener(new View.OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    // do nothing, just consume ACTION_UP event
                    return true;
                }
                return false;
            }

        });
        regularTeamNickname.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE
                        || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    complete();
                    return true;
                } else {
                    return false;
                }
            }
        });
        
        regularTeamNickname.addTextChangedListener(new TextWatcher()
		{
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(s.length()==0){
					regularTeamNickname.setHint("输入群聊名称");
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});;
    }

    private void complete() {
        if(TextUtils.isEmpty(regularTeamNickname.getText().toString())) {
            Toast.makeText(this, R.string.team_name_toast, Toast.LENGTH_SHORT).show();
        } else {
        	if(regularTeamNickname.getText().toString().trim().contains(" ")){
        		Utils.showToast(this, "群聊名称不能带有空格");
        	}
        	else if(regularTeamNickname.getText().toString().trim().length()>10){
        		Utils.showToast(this, "不能超过10个字");
        	}else{      	
            Intent intent = getIntent();
            intent.putExtra(EXTRA_NAME, regularTeamNickname.getText().toString());
            setResult(RESULT_OK, intent);
            showKeyboard(false);
            finish();
        	}
        }

    }

    @Override
    public void onBackPressed() {
        showKeyboard(false);
        super.onBackPressed();
    }
	protected void showKeyboard(boolean isShow) {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (isShow) {
			if (getCurrentFocus() == null) {
				imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
			} else {
				imm.showSoftInput(getCurrentFocus(), 0);
			}
		} else {
			if (getCurrentFocus() != null) {
				imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
	}

	@Override
    public void onClick(View v) {
	    switch(v.getId()){
	    	case R.id.common_header_back:
	    		showKeyboard(false);
	    		finish();
	    		break;
	    	case R.id.common_header_publish:
	    		complete();
	    		break;
	    }
    }
}
